/**
 * Labox有限公司项目InnovationServer
 * Labox版权所有 @copyright 2010
 * @javaclass Svn.java @author 丁宁 @time 2010-7-22
 */
package labox.innovation.loginserver.svn;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import labox.innovation.config.Config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Administrator
 * 
 */
public class Svn {

	protected static final Logger _log = LoggerFactory.getLogger(Svn.class.getName());

	private HashMap<Integer, ArrayList<History>> _svnmap;

	public static class Data {

		int _svn;

		byte[] _data;

		public Data(int svn, byte[] data) {
			_svn = svn;
			_data = data;
		}

		public int getInt() {
			return _svn;
		}

		public byte[] getData() {
			return _data;
		}
	}

	public static class Resouce {
		int _svn;

		ArrayList<Data> _data;

		public Resouce(int svn, ArrayList<Data> data) {
			_svn = svn;
			_data = data;
		}

	}

	private int _curVer;

	protected ArrayList<Resouce> _data = new ArrayList<Resouce>();

	public Svn() {
		//打开历史版本
		openSvnHistory();

		int maxVer = -1;
		for (int i : _svnmap.keySet()) {
			if (i > maxVer) {
				maxVer = i;
			}
		}

		_data.add(new AnimationModelSvn().load());
		_data.add(new AnimationImageSvn().load());
		_data.add(new MapImageSvn().load());

		_curVer = maxVer;

		ArrayList<History> his = _svnmap.get(maxVer);

		for (int i = -1; ++i < _data.size();) {
			if (his == null || i >= his.size() || _data.get(i)._svn != his.get(i)._svn) {
				_curVer++;
				break;
			}
		}

		
		if (_curVer != maxVer) {
			
			ArrayList<History> al = new ArrayList<History>();

			for (int i = -1; ++i < _data.size();) {
				Resouce r = _data.get(i);
				History h = new History(r._svn);
				for (int j = -1; ++j < r._data.size();) {
					h.addVer(r._data.get(j)._svn);
				}
				al.add(h);
			}

			_svnmap.put(_curVer, al);
			closeSvnHistory();
		}

	}

	public Map<Integer, ArrayList<Data>> checkVer(int ver) {
		if (_curVer == ver) {
			return null;
		} else {
			if (_svnmap.containsKey(ver)) {
				//获得老版本的数据版本情况
				ArrayList<History> old = _svnmap.get(ver);

				HashMap<Integer, ArrayList<Data>> map = new HashMap<Integer, ArrayList<Data>>();

				int size = _data.size();
				for (int i = -1; ++i < size;) {
					Resouce r = _data.get(i);
					if (i >= old.size()) {//如果有新添加的资源，直接加入新的资源不需要判定
						ArrayList<Data> al = new ArrayList<Data>();
						map.put(i, al);
						for (int j = -1; ++j < r._data.size();) {//直接加入新的资源
							Data d = r._data.get(j);
							al.add(new Data(j, d._data));
						}
					} else if (r._svn != old.get(i)._svn) {//如果发现版本一样则更新
						History h = old.get(i);
						ArrayList<Data> al = new ArrayList<Data>();
						map.put(i, al);
						for (int j = -1; ++j < r._data.size();) {
							Data d = r._data.get(j);
							if (j >= h._dataSvn.size() || d._svn != h._dataSvn.get(j)) {//判断新加入的资源和修改的资源
								al.add(new Data(j, d._data));
							}
						}
					}
				}
				return map;
			} else {
				int size = _data.size();
				HashMap<Integer, ArrayList<Data>> map = new HashMap<Integer, ArrayList<Data>>();
				for (int i = -1; ++i < size;) {
					Resouce r = _data.get(i);

					ArrayList<Data> al = new ArrayList<Data>();
					for (int j = -1; ++j < r._data.size();) {
						al.add(new Data(j, r._data.get(j)._data));
					}

					map.put(i, al);
				}
				return map;
			}
		}

	}

	public int getVer() {
		return _curVer;
	}

	public void openSvnHistory() {
		try {
			File file =new File("./svn.map");
			if(file.exists()){
				ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
				_svnmap = (HashMap<Integer, ArrayList<History>>) ois.readObject();
				ois.close();
				_log.info("resouceload:load svn map ok",Config.SERVER_ID);
			}else{
				_log.info("resouceload:no find svn map file,fail to load svn map",Config.SERVER_ID);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (_svnmap == null)
			_svnmap = new HashMap<Integer, ArrayList<History>>();
	}

	public void closeSvnHistory() {
		try {
			if (_svnmap == null)
				return;
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./svn.map"));
			oos.writeObject(_svnmap);
			oos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
